package ldy.es;

import ldy.es.utils.Utils;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Map;

/**
 * 包名. ldy.es
 * Copyright tiens. All Rights Reserved.
 * 描述详情：elasticsearch 构造器
 * 创建者：liudy
 * 创建时间：2018/4/3-11:21.
 */
@Component
public class ElasticsearchBuilder {

    public  void createIndexIfNotCreated(String indexName) {
        if(!isIndexExists(indexName)){
            ElasticSearchConnector.getClient().admin().indices().prepareCreate(indexName).execute().actionGet();
        }
    }
    public  void createIndexIfNotCreated(String indexName, int shards, int replicas) {
        if(!isIndexExists(indexName)){
            ElasticSearchConnector.getClient().admin().indices().prepareCreate(indexName)
                    .setSettings(Settings.builder()
                            .put("index.number_of_shards", shards)
                            .put("index.number_of_replicas", replicas)
                    ).get();
        }
    }

    public  void createIndexAndTypeMapping(String indexName, int shards, int replicas, String typeName, String mapping) {
        if(!isIndexExists(indexName)){
            ElasticSearchConnector.getClient().admin().indices().prepareCreate(indexName)
                    .setSettings(Settings.builder()
                            .put("index.number_of_shards", shards)
                            .put("index.number_of_replicas", replicas)
                    )
                    .addMapping(typeName, mapping, XContentType.JSON)
                    .get();
        }
    }
    public  void createIndexAndTypeMapping(String indexName, int shards, int replicas, String typeName, Map<String, Object> mapping) {
        if(!isIndexExists(indexName)){
            ElasticSearchConnector.getClient().admin().indices().prepareCreate(indexName)
                    .setSettings(Settings.builder()
                            .put("index.number_of_shards", shards)
                            .put("index.number_of_replicas", replicas)
                    )
                    .addMapping(typeName, mapping)
                    .get();
        }
    }

    public boolean isIndexExists(String indexName) {
        IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);
        IndicesExistsResponse inExistsResponse = ElasticSearchConnector.getClient().admin().indices()
                .exists(inExistsRequest).actionGet();
        return inExistsResponse.isExists();
    }
    /**
     * 插入index的type和mapping结构
     * @param indexName 存在的索引名称
     * @param typeName 待建立的type名称
     * @param mapping type结构文件
     * @ 创建人：liudy
     * 创建时间：2018/4/5-14:55
     */
    public void addTypeAndMapping(String indexName, String typeName, String mapping){
        ElasticSearchConnector.getClient().admin().indices().preparePutMapping(indexName)
                .setType(typeName)
                .setSource(mapping, XContentType.JSON)
                .get();
    }

    public void updateIndexReplicas(String indexName,  int replicas){
        ElasticSearchConnector.getClient().admin().indices().prepareUpdateSettings(indexName)
                .setSettings(Settings.builder()
                        .put("index.number_of_replicas", replicas)
                )
                .get();
    }

}
